<?php

namespace app\commission;

use app\models\user\UserAccount;
use app\models\user\UserCommission;
use app\models\user\UserSignIn;

/**
 * 签到送积分
 *
 * Class SignIn
 * @package app\commission
 */
class SignIn
{
    /**
     * 签到积分
     * (后台设置积分)
     *
     * @param UserSignIn $signIn
     * @param int $score
     */
    public function run(UserSignIn $signIn, $score = 0)
    {
        // commission
        $commission = new UserCommission();
        $commission->user_id = $signIn->user_id;
        $commission->fans_id = $signIn->user_id;
        $commission->score = $score;
        $commission->sign_in_id = $signIn->id;
        $commission->award_type = UserCommission::$AWARD_Type['SIGN_IN'];
        $commission->award_name = UserCommission::$AWARD_NAME['SIGN_IN'];
        $commission->status = 1;
        $commission->save();

        // account
        $account = UserAccount::findOne(['user_id' => $signIn->user_id]);
        $account->score += $score;
        $account->save();
    }
}